class BaseList {
    constructor(orderby, flag, $pagesize, $pagination, listUrl, onLineEditUrl, checkBoxName) {
        this.first = 0;
        this.orderby = orderby;
        this.flag = flag;
        this.pagesize = $pagesize;
        this.pagination = $pagination;
        this.pagenavbar = $("#wrapper__navbar");
        this.listUrl = listUrl;
        this.onLineEditUrl = onLineEditUrl;
        this.limit = 15;
        this.numCheckbox = 0;
        this.selectedItemList = [];
        this.checkBoxName = checkBoxName;
        this.loading__indicator = $("#loading__indicator");
        this.loading__indicator_i = $("#loading__indicator i");
    }
    SetPageSize() {
        this.FetchForPage(1);
    }
    OnPageLoad() {
        if (parseInt(this.flag) === 0) {
            // 如果是单选情况，那么隐藏【确定】按钮
            $("#ButOk").remove();
        }
        if (parseInt(this.flag) === 1) {
            // 设置全选框
            let html = '<label class="btn" for="CheckAll" style="margin-left:6px;"><input id="CheckAll" type="checkbox" onclick="_Instance.DoCheckAll($(this));" /><i></i><span></span></label>';
            $("#th-checkall").text('');
            $(html).appendTo($("#th-checkall"));
        }
        this.FetchForPage(1);
    }
    GetFilter(pageIndex) {
        if (pageIndex !== 0 && this.pagination !== null) {
            this.pagination.attr("_msh_page", pageIndex);
        }
        let item = {};
        item['Limit'] = this.limit;
        item['PageIndex'] = this.pagination === null ? 0 : parseInt(this.pagination.attr("_msh_page"));
        item['OrderBy'] = this.orderby;

        return item;
    }
    FetchForPage(pageIndex) {
        $.ajax({
            type: "POST",
            url: parent.basePath + this.listUrl,
            timeout: 30 * 1000,
            data: this.GetFilter(pageIndex),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: () => {
                // 显示刷新动画
                this.loading__indicator_i.addClass("fa-spin");
                this.loading__indicator.show();
            }
        }).done(data => {
            if (data instanceof Array) {
                this.SetTableContent(data);
            } else {
                if (data.Success === true) {
                    this.BuildDataTable(data);
                } else {
                    this.EmptyTableContent();
                    $('#loading').hide();
                }
            }
        }).fail(data => {
            $('#loading').hide();
            ajaxError(data, 1);
        }).always(() => {
            this.first = 1;
            // 隐藏刷新动画
            this.loading__indicator_i.removeClass("fa-spin");
            this.loading__indicator.hide();
        });
    }
    // 得到目录树的当前路径
    GetTreePath(nodeIdOfSelected) {
        let arrTreePath = new Array();
        arrTreePath.push(nodeIdOfSelected);
        let ref = $.jstree.reference('treeContent');
        let _nodeOfSelected = $("#" + nodeIdOfSelected);

        while (ref.get_parent(_nodeOfSelected) !== "#") {
            arrTreePath.push(ref.get_parent(_nodeOfSelected));
            nodeIdOfSelected = ref.get_parent(_nodeOfSelected);
            _nodeOfSelected = $("#" + nodeIdOfSelected);
        }
    }
    BuildDataTable(result) {
        if (result.TotalPages !== undefined) {
            // 设置翻页控件
            if (result.TotalPages > 0) {  //总页数
                this.pagenavbar.data("swPagination").options.max_page = result.TotalPages;
            } else {
                this.pagenavbar.data("swPagination").options.max_page = 1;
            }
            if (result.PageIndex > 0) { //当前页数
                this.pagenavbar.data("swPagination").options.current_page = result.PageIndex;
                this.pagenavbar.data("swPagination").setPage(result.PageIndex);
            } else {
                this.pagenavbar.data("swPagination").options.current_page = 1; //当前页数
                this.pagenavbar.data("swPagination").setPage(1);
            }
            // 得到数据列表
            result = result.DataList;
        }
        if (result === null) {
            $('#loading').hide();
            return false;
        }
        this.SetTableContent(result);

        $(window).resize();               // 防止因为出现滚动条而造成画面显示问题
        $('#loading').hide();
    }
    SetTableContent(result) {
        return result;
    }
    DoCheckAll($self) {
        let _name = "input[type='checkbox'][name='" + this.checkBoxName + "']";
        let _this = this;
        if ($self.is(":checked") === true) {
            $(_name).each(function () {
                $(this).prop('checked', true);
                _this.SetRowStatus($(this));
            });
        } else {
            $(_name).each(function () {
                $(this).prop("checked", false);
                _this.SetRowStatus($(this));
            });
        }
    }
    DoCheckRow($self, $checkall) {
        let _name = "input[type='checkbox'][name='" + this.checkBoxName + "']:checked";
        let _checkedRows = $(_name).length;
        if (_checkedRows < this.numCheckbox) {
            $checkall.prop("checked", false);
        } else {
            $checkall.prop("checked", true);
        }
        this.SetRowStatus($self);
    }
    AdjustRowStatus($self, disablerow, enablerow) {
        let $row = $self.parent().parent().parent();
        if ($self.is(":checked") === true) {
            $row.addClass("row__selected");
            disablerow($row, $row.index());
        } else {
            $row.removeClass("row__selected");
            enablerow($row, $row.index());
        }
    }
    // 选择后执行的动作
    OnChoice($self) {
        let item = this.SetChoiceData($self);
        window.parent.arrChoice.push(item);
        window.parent.choice_success = true;
        closePopupWnd();
    }
    // 多选后结果
    OnMultiChoice() {
        let result = [];
        // 判断是否做了选择
        if (Object.keys(this.selectedItemList).length > 0) {
            for (let key in this.selectedItemList) {
                result.push(this.selectedItemList[key]);
            }
            parent.arrChoice.push(result);
            parent.choice_success = true;
        } else {
            parent.choice_success = false;
        }

        closePopupWnd();
    }
    OnlineEdit() {
        location = parent.basePath + this.onLineEditUrl;
    }
    SortTable(order_by) {
        this.orderby = order_by;
        this.FetchForPage(1);
    }
}